package io.renren.modules.sys.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.entity.AgentEntity;
import io.renren.modules.sys.service.AgentService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 代理商
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2019-10-11 10:46:27
 */
@RestController
@RequestMapping("sys/agent")
public class AgentController {
    @Autowired
    private AgentService agentService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:agent:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = agentService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/businesslist")
    public R businesslist(@RequestParam Map<String, Object> params){
        PageUtils page = agentService.queryBusinessPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:agent:info")
    public R info(@PathVariable("id") Integer id){
        AgentEntity agent = agentService.getById(id);

        return R.ok().put("agent", agent);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:agent:save")
    public R save(@RequestBody AgentEntity agent){
        agent.setCreatedTime(new Date());
        agentService.save(agent);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("sys:agent:update")
    public R update(@RequestBody AgentEntity agent){
        ValidatorUtils.validateEntity(agent);
        agentService.updateById(agent);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("sys:agent:delete")
    public R delete(@RequestBody Integer[] ids){
        agentService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/deleteById")
    public R deleteById(@RequestBody Integer id){
        agentService.removeById(id);

        return R.ok();
    }

    /**
     * 获取代理商密码线条数
     */
    @RequestMapping("/queryLineCodeNum/{id}")
    @RequiresPermissions("sys:agent:queryLineCodeNum")
    public R queryLineCodeNum(@PathVariable("id") Integer id){
        int num = agentService.queryLineCodeNum(id);

        return R.ok().put("num", num);
    }

    /**
     * 获取业务员密码线条数
     */
    @RequestMapping("/querySaleLineCodeNum/{id}")
    @RequiresPermissions("sys:agent:querySaleLineCodeNum")
    public R querySaleLineCodeNum(@PathVariable("id") Integer id){
        int num = agentService.querySaleLineCodeNum(id);

        return R.ok().put("num", num);
    }

    /**
     * 通过业务员查找上级代理商
     */
    @RequestMapping("/queryAgentBySaleid/{id}")
    public R queryAgentBySaleid(@PathVariable("id") Integer id){
        SysUserEntity agent = agentService.queryAgentBySaleid(id);

        return R.ok().put("agent", agent);
    }

}
